package com.anbinh.doctor.fragment.account;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.AssistantManagerAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ResponseAssistants;
import com.anbinh.doctor.model.Address.Data.Item;
import com.anbinh.doctor.model.ResponseAssistants.Data.Assistants;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAssistantList extends Fragment {
	private static FragmentAssistantList fragment = null;
	private View mView;
	private CircleProgressBar loadding;
	private AssistantManagerAdapter adapter;
	private ListView mGridView;
	protected ArrayList<Assistants> mList;
	private Assistants currentAssistants;

	public static FragmentAssistantList newInstance() {
		if (fragment == null) {
			fragment = new FragmentAssistantList();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentAssistants = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.ASSISTANT_DETAILT);
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				bundle.putBoolean(Utils.KEY_UPDATE, true);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.tro_ly));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});

		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData();
	}

	public Assistants getCurrentAssistants() {
		return currentAssistants;
	}

	public void onLoadData() {
		loadding.setVisibility(View.VISIBLE);
		final UserLogin user = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = user.getData().getToken();

		ServerController.getInstance(getActivity()).reuquestListAssistants(
				token, new OnRequest<ResponseAssistants>() {

					@Override
					public void onSuccess(ResponseAssistants data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;
						if (data.getData().getAssistants() == null
								|| data.getData().getAssistants().size() < 0)
							return;
						mList = data.getData().getAssistants();
						adapter = new AssistantManagerAdapter(getActivity(),
								R.layout.item_doctor, mList, new IRcallBak() {

									@Override
									public void onDisplay(int type, Bundle data) {
										showPopupMenu(mList.get(type));
									}
								});
						mGridView.setAdapter(adapter);

					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}

	public void showPopupMenu(final Assistants item) {
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.abs__menu_dropdown_panel_holo_light);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) getActivity()).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_menu_location);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView lblSetDefault = (TextView) dialog
				.findViewById(R.id.lblSetDefault);
		lblSetDefault.setVisibility(View.GONE);

		TextView lblXoaDc = (TextView) dialog.findViewById(R.id.lblXoaDc);
		lblXoaDc.setText("Xóa trợ lý " + item.getFullname());
		lblXoaDc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				onDelete(item);
			}
		});
		dialog.show();
	}

	protected void onDelete(final Assistants item) {
		Utils.onLoadding(mView, true);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		if (userLogin == null)
			return;

		ServerController.getInstance(getActivity()).requestAssistiantRemove(
				userLogin.getData().getToken(), item.getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						Utils.onLoadding(mView, false);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() >= 0) {
							mList.remove(item);
							adapter.notifyDataSetChanged();
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.onLoadding(mView, false);
					}
				});
	}
}
